/*
 * =====================================================================================
 *
 *       Filename:  NbtPacket.cc
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  2012年02月14日 16时40分00秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Regan (), lcqhigh@gmail.com
 *        Company:
 *
 * =====================================================================================
 */
#include "NbtPacket.h"


NbtPacket::NbtPacket(char *buf, int buflen)
    :buf_(buf),
     buflen_(buflen)
{
}

NbtPacket::~NbtPacket()
{
}


    //获取与设置包的类型
bool NbtPacket::isRequestType()
{
   return (CVAL(buf_, 2) & 0x80) == 0;
}

bool NbtPacket::isResponceType()
{
    return (CVAL(buf_, 2) & 0x80) == 0;
}
//void NbtPacket::setTypeToRequest()
//void NbtPacket::setTypeToResponce()

void NbtPacket::setHeaderLine(int32_t id, unsigned char pcode, unsigned char nmflag, unsigned char recode)
{
    SSVAL(buf_, 0, id);
    CVAL(buf_, 2) = (pcode << 3) | ((nmflag >> 4)&0x07);
    CVAL(buf_, 3) = (nmflag << 4) | (recode & 0xF);
    //SSVAL(buf_, 4, qdcout);
    //SSVAL(buf_, 10, arcount);
}

uint16_t NbtPacket::32_t transId()
{
    return SVAL(buf_, 0);
}
//void setTransId(int32_t id)

unsigned char NbtPacket::pcode()
{
    return (CVAL(buf_, 2) >> 3) & 0x1f;
}
//void setPCode(unsigned char pcode)

int NbtPacket::resourceCount(uint16_t& qdcout, uint16_t& acount, uint16_t& nscount, uint16_t& arcount)
{
    qdcout = SVAL(buf_, 4);
    acount = SVAL(buf_, 6);
    nscount = SVAL(buf_, 8);
    arcount = SVAL(buf_, 10);
}

void setResourceCount(uint16_t qdcout, uint16_t acount, uint16_t nscount, uint16_t arcount)
{
    SSVAL(buf_, 4, qdcout);
    SSVAL(buf_, 6, account);
    SSVAL(buf_, 8, nscout);
    SSVAL(buf_, 10, arcount);
}


unsigned char NbtPacket::NM_Flags()
{
    return ((CVAL(buf_, 2) & 0x07) << 4) | 
            (CVAL(buf_,3) >> 4)
}
//void setNM_Flags(unsigned char flag)

unsigned char NbtPacket::returnCode()
{
    return (CVAL(buf_, 3) & 0x0f);
}

char *NbtPacket::name()
{
    return buf_+12;
}

void NbtPacket::setName(char *name, int len)
{
    NbtUtil::name_mangle(name, buf_+12);
}

unsigned char NbtPacket::NB_Flags()
{

}

void NbtPacket::setNB_Flags(unsigned char flag)
{

}
